using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;

using System.Linq;
using System.Xml;
using System.Xml.Linq;
using System.Windows.Forms;
using System.Reflection;
using GuavaData;
using GuavaLib;
using System.Net.Sockets;
using System.IO;



namespace CORIDemo
{
    public partial class Lobby : GForm
    {
        private Dictionary<Guid, string> launchInfo;
        private List<Guid> ids;
        private Assembly asm;

        public Lobby()
        {
            InitializeComponent();
        }

        public Lobby(Guid id, Guid fk, bool bNew, GSeed gs)
            : this()
        {
            asm = Assembly.GetCallingAssembly();

            launchInfo = new Dictionary<Guid, string>();
            ids = new List<Guid>();
            this.Init(id, fk, bNew, gs);
            this.qryInterface.Init(AppData.gt);
        }

        private void btnDebug_Click(object sender, EventArgs e)
        {
            DataSet ds = AppData.ds;
            AppData.c.RunTestSuite(this.DebugRows);
        }

        private void AC_Click(object sender, EventArgs e)
        {
            XmlDocument doc = null;
            DataSet ds = null;

          //  DataSet ds = AppData.ds;
            //AppData.c.RunTestSuite();
           // McServer.Init(null, "INYETI", "guava");
            //XmlDocument doc = McServer.queryDB("test");
            //doc.Save("results.xml");
        }

        private void search()
        {
            if (this.checkBox1.Checked || this.checkBox2.Checked || this.checkBox3.Checked)
            {
                String from = "from procedurepicker pp, patient p ";
                String select = "select p.firstname as firstname, p.lastname as lastname, pp.form as form, pro.proceduredate as proceduredate, pp.id as id ";
                String where = "where pro.id=pp.id and p.id=pp.fk and pro.ProcedureDate between '" + this.dateTimePicker1.Value.ToShortDateString() + " " + this.dateTimePicker1.Value.ToShortTimeString() + "' and '" + this.dateTimePicker2.Value.ToShortDateString() + " " + this.dateTimePicker2.Value.ToShortTimeString() + "' order by pro.proceduredate";

                List<string> procedureList = new List<string>();
                if (this.checkBox3.Checked)
                {
                    procedureList.Add(checkBox1.Text);
                    procedureList.Add(checkBox2.Text);
                }
                else
                {
                    if (checkBox1.Checked)
                        procedureList.Add(checkBox1.Text);
                    if (checkBox2.Checked)
                        procedureList.Add(checkBox2.Text);
                }
                if (procedureList.ToArray().Length == 2)
                {
                    from = from + ", ((select id, proceduredate from " + procedureList[0] + ") union ( select id, proceduredate from " + procedureList[1] + ")) as pro ";
                }
                else from = from + ",(select id, proceduredate from " + procedureList[0] + ") as pro ";

                string sql = select + from + where;
                DataSet ds = AppData.c.PoseRawQuery(sql);
                this.procedures.Items.Clear();
                launchInfo.Clear();
                ids.Clear();
                if (ds.Tables.Count > 0)
                {
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        procedures.Items.Add(dr["proceduredate"] + ": " + dr["form"] + ": " + dr["lastname"] + ", " + dr["firstname"]);
                        launchInfo.Add((Guid)dr["id"], dr["form"].ToString());
                        ids.Add((Guid)dr["id"]);
                    }
                }
            }
            else
            {
                return;
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            search();
 
        }

        private void procedures_DoubleClick(object sender, EventArgs e)
        {
            int index = this.procedures.SelectedIndex;
            if (ids.Count > index)
            {

                LaunchForm(ids[index], launchInfo[ids[index]], false);
            }

        }
        public DialogResult LaunchForm(Guid gid, string form, bool bNew)
        {
            DialogResult d;

            Type t = GLaunchClass.RetrieveTypeFromFile(form, this.asm);
            if (t == null) return DialogResult.Cancel;

            ConstructorInfo ci = t.GetConstructor(new Type[] { typeof(Guid), typeof(Guid), typeof(bool), typeof(GSeed) });

            GForm o = ci.Invoke(new object[] { gid, gid, bNew, null}) as GForm;


            //this.PrepareStackFrame();
            d = o.ShowDialog();

            // When done, refresh the items
            search();
            return d;
        }




    }
}